package com.atguigu.edu.realtime.Base;

import com.atguigu.edu.realtime.common.Constant;
import com.atguigu.edu.realtime.utils.SQLUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION;

public abstract class BaseSQLApp {
    protected abstract void handle(StreamExecutionEnvironment env, StreamTableEnvironment tEnv);


    public void init(int port, int p, String ckAndJobName) {
        System.setProperty("HADOOP_USER_NAME","atguigu");
        //读取 ods_db 数据
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        //并行度的设置原则：和kafka的topic的分区数保持一致
        env.setParallelism(p);
        //开启checkpoint，严格一次
        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        //启用checkpoint并设置checkpoint的周期
        env.enableCheckpointing(3000);
        //设置checkpoint的语义：严格一次
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        //设置checkpoint的存储目录
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/edu/" + ckAndJobName);
        //超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);
        //checkpoint之间的间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        //当job取消的时候，checkpoint的保留策略:当job手动取消时，将会保留作业的 Checkpoint 状态信息
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(RETAIN_ON_CANCELLATION);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.getConfig().getConfiguration().setString("pipeline.name", ckAndJobName);

        handle(env, tEnv);
    }

    public void readOdsDb(StreamTableEnvironment tEnv, String groupId) {
        //通过ddl 读取ods_db数据
        tEnv.executeSql("create table ods_db(" +
                " `database` string," +
                " `table` string," +
                " `type` string," +
                " `data` map<string,string>," +
                " `old` map<string,string>," +
                " `ts` bigint," +
                " `pt` as proctime()," +
                " `et` as to_timestamp_ltz(ts,0)," +
                " watermark for `et` as `et` - interval '3' second " +
                ")" + SQLUtil.getKafkaSourceDDL(Constant.TOPIC_ODS_DB, groupId, "json"));

    }
}
